package com.example.administrator.jingpingou.ui;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.administrator.jingpingou.R;
import com.example.administrator.jingpingou.bean.CommodityBean;
import com.example.administrator.jingpingou.bean.TeJiaBean;
import com.example.administrator.jingpingou.config.NetConfig;

import org.xutils.DbManager;
import org.xutils.config.DbConfigs;
import org.xutils.ex.DbException;
import org.xutils.x;

public class TeJiaDetailActivity extends AppCompatActivity {
    private WebView mWebView;
    private String num_iid;
    private ImageView mImageViewAdd,ivBack,ivGo,ivFresh;
    private TeJiaBean.ListEntity listEntity;
    private DbManager mDb;
    private String url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_te_jia_detail);
        iniView();
        iniData();
        setData();
        setListener();
    }

    private void setListener() {
        mImageViewAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listEntity!=null) {
                    CommodityBean comm=new CommodityBean();
                    comm.setTitle(listEntity.getTitle());
                    comm.setDiscount(listEntity.getDiscount()+"");
                    comm.setNow_price(listEntity.getNow_price()+"");
                    comm.setOrigin_price(listEntity.getOrigin_price()+"");
                    comm.setPic_url(listEntity.getPic_url());

                    comm.setCount(1+"");
                    try {
                        mDb.save(comm);
                    } catch (DbException e) {
                        e.printStackTrace();
                    }
                    Toast.makeText(TeJiaDetailActivity.this, "加入购物车成功", Toast.LENGTH_SHORT).show();

                }

            }
        });
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                finish();
            }
        });
        ivGo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(TeJiaDetailActivity.this, "不能再向后了", Toast.LENGTH_SHORT).show();
            }
        });
        ivFresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mWebView.loadUrl(url);
            }
        });
    }

    private void iniData() {
        Intent intent = getIntent();
        num_iid=intent.getStringExtra("num_iid");
        listEntity= (TeJiaBean.ListEntity) intent.getSerializableExtra("listEntity");
        DbManager.DaoConfig config=new DbManager.DaoConfig();
        mDb= x.getDb(config);

    }


    private void setData() {
       url=String.format(NetConfig.TEJIA_DETAIL_PATH,num_iid);
        Log.i("TAG", "web==url"+url);
        mWebView.loadUrl(url);
        mWebView.setWebViewClient(new WebViewClient());
        mWebView.setWebChromeClient(new WebChromeClient());
        WebSettings settings = mWebView.getSettings();
        settings.setJavaScriptEnabled(true);

    }

    private void iniView() {
        mWebView= (WebView) findViewById(R.id.wv_tejia_detail_show);
        mImageViewAdd= (ImageView) findViewById(R.id.iv_tejia_detail_add);
        ivGo= (ImageView) findViewById(R.id.iv_tejia_detail_go);
        ivFresh= (ImageView) findViewById(R.id.iv_tejia_detail_refresh);
        ivBack= (ImageView) findViewById(R.id.iv_tejia_detail_back);
    }

    @Override
    protected void onPause() {
        super.onPause();
        overridePendingTransition(0,R.anim.activity_finish);
    }
}
